use tonic_prost_build::Builder;

pub trait BuilderAttributes {
    fn with_path_attributes(self, path: &str, attributes: &[&str]) -> Self;
    fn with_paths_attributes(self, path: &[&str], attributes: &[&str]) -> Self;
    fn with_path_field_attributes(self, path: &str, field: &str, attributes: &[&str]) -> Self;
    fn with_path_fields_attributes(self, path: &str, fields: &[&str], attributes: &[&str]) -> Self;
    fn with_paths_fields_attributes(
        self,
        paths: &[&str],
        fields: &[&str],
        attributes: &[&str],
    ) -> Self;
}

impl BuilderAttributes for Builder {
    fn with_path_attributes(self, path: &str, attributes: &[&str]) -> Self {
        let attr = attributes.join("\n");
        self.type_attribute(path, attr.as_str())
    }

    fn with_paths_attributes(self, path: &[&str], attributes: &[&str]) -> Self {
        path.iter().fold(self, |builder, path| {
            builder.with_path_attributes(path, attributes)
        })
    }

    fn with_path_field_attributes(self, path: &str, field: &str, attributes: &[&str]) -> Self {
        let attr = attributes.join("\n");
        self.field_attribute(format!("{path}.{field}"), attr.as_str())
    }

    fn with_path_fields_attributes(self, path: &str, fields: &[&str], attributes: &[&str]) -> Self {
        fields.iter().fold(self, |builder, field| {
            builder.with_path_field_attributes(path, field, attributes)
        })
    }

    fn with_paths_fields_attributes(
        self,
        paths: &[&str],
        fields: &[&str],
        attributes: &[&str],
    ) -> Self {
        paths.iter().fold(self, |builder, path| {
            builder.with_path_fields_attributes(path, fields, attributes)
        })
    }
}
